package com.xx.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.entity.PageResult;
import com.xx.entity.Products;
import com.xx.service.IProductsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/products")
@Tag(name = "商品前端控制器",description = "商品接口")
public class ProductsController {
    @Autowired
    IProductsService iProductsService;

    @PostMapping
    @Operation(summary = "新增商品数据", description ="新增商品数据接口描述")
    public int save (@RequestBody Products products){
        return iProductsService.insertProduct(products);
    }

    @DeleteMapping("{productId}")
    @Operation(summary = "删除商品数据", description ="删除商品数据接口描述")
    public int delete(@PathVariable Integer productId){
        return iProductsService.deleteProduct(productId);
    }

    @PutMapping("{productId}")
    @Operation(summary = "修改商品数据", description ="修改商品数据接口描述")
    public int update(@PathVariable Integer productId, @RequestBody Products products){
        products.setProductId(productId);
        return iProductsService.updateProduct(products);
    }

    @GetMapping("{id}")
    @Operation(summary = "通过Id查询商品数据", description ="通过Id查询商品数据接口描述")
    public Products getById(@PathVariable Integer productId){
        return iProductsService.getProductWithSupplier(productId);
    }

    // 获取分页数据
    @GetMapping("page")
    @Operation(summary = "商品分页数据", description ="商品分页数据接口描述")
    @ApiResponses( value = {
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "201", description = "请求失败")
    }

    )
    public PageResult<Products> getEmployeePage(
            @Parameter(name = "current", description = "当前页码", required = true) @RequestParam int current,
            @Parameter(name = "size", description = "每页显示条数", required = true)
            @RequestParam(value = "size", defaultValue = "3") // 默认每页显示5条数据
            int size) {
        Page<Products> page = new Page<>(current, size);
        Page<Products> resultPage = iProductsService.getProductPageWithSupplier(page);
        return PageResult.of(resultPage);
    }

}
